﻿using OneOfAll.Common.Models;
using OneOfAll.Common.Util;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media.Animation;

namespace OneOfAll
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            //   Timeline.DesiredFrameRateProperty.OverrideMetadata(
            //typeof(Timeline),
            //new FrameworkPropertyMetadata { DefaultValue = 30 }
            //);

            //非UI线程异常
            this.DispatcherUnhandledException += App_DispatcherUnhandledException;
            //TASK线程异常
            TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
            ExceptionHelper.WarnEvent += ExceptionHelper_WarnEvent;
            base.OnStartup(e);
        }

        private void ExceptionHelper_WarnEvent(string addr, string msg)
        {
            this.Dispatcher.Invoke(() =>
            {
                MessageBox.Show($"{addr}:{msg}");
            });
        }

        private void EncryptionManager_ErrorEvent(string msg)
        {
            this.Dispatcher.Invoke(() =>
            {
                MessageBox.Show(msg);
            });
        }


        private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
        {
            LogHelper.Instance.GetLog(LogTypeEnum.Error, e.Exception.Message);
            Dispatcher.Invoke(() =>
            {
                MessageBox.Show(e.Exception.Message);
                e.SetObserved();
            });
        }

        private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            LogHelper.Instance.GetLog(LogTypeEnum.Error, e.Exception.Message);
            Dispatcher.Invoke(() =>
            {
                MessageBox.Show(e.Exception.Message);
                e.Handled = true;
            });
        }

    }
}
